import { Component } from '@angular/core';
import {IonicPage, NavController, NavParams, ViewController} from 'ionic-angular';
import {PageParam} from "../../system/models/page-param-model";
import {AppService} from "../../system/services/app.service";
import {ResDataModel} from "../../system/models/res-data-model";

/**
 * Generated class for the MyListDemoPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-my-list-demo',
  templateUrl: 'my-list-demo.html',
})
export class MyListDemoPage {

  //后端服务Ctrl
  ctrl: string = 'AppDemoUserCtrl_list';
  //分页参数
  pageParam = new PageParam();
  //业务请求参数
  postData = {
    userName:'',
    ancestorSfjgDm:'',
  };
  //是否还有更多数据
  hasMore = true;
  //列表
  items: any[];


  //依赖注入
  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              public viewCtrl: ViewController,
              public appService: AppService) {
  }

  /**
   * 每次页面进入时执行
   */
  ionViewDidEnter() {
    this.pageParam.pageNum = 1;//将页码初始化为1
    this.getItems();
  }

  /**
   * 获取列表数据
   */
  getItems(){
    this.appService.httpPost(this.ctrl,this.postData,this.pageParam,(res:ResDataModel) =>{
      //打印调试
      console.log(res.datas);
      //将获取的结果数据赋值给items
      this.items = res.datas[0].trs;
      //翻页参数页码自增
      this.pageParam.pageNum += 1;
    },true)
  }

  /**
   * 上拖分页查询加载
   * @param infiniteScroll
   */
  doInfinite(infiniteScroll) {
    if (this.hasMore == false) {//没有更多数据
      infiniteScroll.complete();
      return;
    }
    //获取数据
    this.appService.httpPost(this.ctrl,this.postData,this.pageParam,(res:ResDataModel) =>{
      if(res.datas[0].trs.length > 0) {
        //增量复制给items
        this.items = this.items.concat(res.datas[0].trs);
        //页码自增
        this.pageParam.pageNum += 1;
      } else {
        this.hasMore = false;
      }
      infiniteScroll.complete();
    })
  }

  goDetails(item) {
    //console.debug('go details:' + type);
    this.navCtrl.push('MyDetailDemoPage', { item: item });
  }

  /**
   * 模态框退出
   */
  dismiss() {
    this.viewCtrl.dismiss();
  }
}
